package com.teach;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

public class Demo02 {

	public static void main(String[] args)throws Exception {
		File file = new File("d:/upload/out3.txt");
		try(
//			Writer writer = new FileWriter(file);//非附加方式
			Writer writer = new FileWriter(file, true);//附加方式
//			FileOutputStream fileOutputStream = new FileOutputStream(file, true);
		){
			writer.write(65);
			writer.write(new char[] {'a','b','c'});
			writer.write("中华人民共和国");
			writer.append('z');
			writer.append("新加的内容");
			
		}
		
		try(
			Writer writer = new FileWriter(file, true);//附加方式
			BufferedWriter bufferedWriter = new BufferedWriter(writer);	
		){
			bufferedWriter.write("\n这是使用缓冲写入流新增的内容");
			bufferedWriter.flush();
		}

		try(
			OutputStream out = new FileOutputStream(file, true);//附加方式
			Writer writer = new OutputStreamWriter(out, "GBK");
			BufferedWriter bufferedWriter = new BufferedWriter(writer);	
		){
			bufferedWriter.write("\n这是使用字节输出流新增的内容");
			bufferedWriter.flush();
		}
		
		System.out.println("---------------------------自己的工具类");
		String str = "我们要写入的内容";
		com.teach.util.FileUtil.writeText(str, new File("d:/upload/out4.txt"), "UTF-8");
		System.out.println("---------------------------hutool工具类");
		cn.hutool.core.io.FileUtil.writeString(str, new File("d:/upload/out5.txt"), "UTF-8");
		cn.hutool.core.io.FileUtil.appendString(str, new File("d:/upload/out5.txt"), "GBK");

		System.out.println("---------------------------");
		PrintStream out =System.out;//打印字节输出流
		OutputStream outputStream = new FileOutputStream("d:/upload/out5.txt");
		PrintStream printStream = new PrintStream(outputStream);
		System.out.println("hello");
		printStream.println("hello");
		System.out.println(true);
		printStream.println(true);
//		System.out.write(b);
		printStream.flush();
		
		PrintWriter printWriter = new PrintWriter(new File("d:/upload/out6.txt"));
		printWriter.println(false);
		printWriter.flush();
		try {
			List<?> list = new ArrayList();
			Object ret = list.get(1);
		}catch (Exception e) {
//			e.printStackTrace();
//			e.printStackTrace(System.err);
//			e.printStackTrace(System.out);
//			e.printStackTrace(printStream);
//			e.printStackTrace(printWriter);
			StringWriter stringWriter = new StringWriter();
			PrintWriter printWriter2 = new PrintWriter(stringWriter);
			e.printStackTrace(printWriter2);
			printWriter2.flush();
			System.out.println(stringWriter.getBuffer());
		}
		printWriter.flush();
		
	}
}
